package com.ailianlian.ablecisi.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ailianlian.ablecisi.baseclass.BaseViewModel;
import com.ailianlian.ablecisi.pojo.entity.AiCharacter;
import com.ailianlian.ablecisi.repository.CharacterRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * AI角色定制的ViewModel
 */
public class CharacterViewModel extends BaseViewModel {

    private MutableLiveData<List<AiCharacter>> aiCharacters;
    private MutableLiveData<Boolean> isLoading;
    private MutableLiveData<Boolean> createSuccess;
    private MutableLiveData<String> errorMessage;
    private CharacterRepository characterRepository;
    private MutableLiveData<AiCharacter> currentCharacter;

    public CharacterViewModel(@NonNull Application application) {
        super(application);
        aiCharacters = new MutableLiveData<>(new ArrayList<>());
        isLoading = new MutableLiveData<>(false);
        createSuccess = new MutableLiveData<>(false);
        errorMessage = new MutableLiveData<>(null);
        currentCharacter = new MutableLiveData<>(null);
        characterRepository = new CharacterRepository(getAppContext());
    }

    /**
     * 加载自定义角色列表
     */
    public void loadCustomCharacters() {
        isLoading.setValue(true);
    }

    /**
     * 创建自定义角色
     * @param aiCharacter 自定义角色对象
     */
    public void createCustomCharacter(AiCharacter aiCharacter) {
        isLoading.setValue(true);
        createSuccess.setValue(false);

    }

    /**
     * 验证角色信息是否有效
     * @param aiCharacter 自定义角色对象
     * @return 错误信息，如果没有错误则返回null
     */
    public String validateCharacter(AiCharacter aiCharacter) {
        if (aiCharacter.getName() == null || aiCharacter.getName().trim().isEmpty()) {
            return "角色名称不能为空";
        }
        if (aiCharacter.getType() == null || aiCharacter.getType().trim().isEmpty()) {
            return "请选择角色类型";
        }
        if (aiCharacter.getGender() == null || aiCharacter.getGender().trim().isEmpty()) {
            return "请选择角色性别";
        }
        if (aiCharacter.getAge() <= 0) {
            return "请输入有效的角色年龄";
        }
        if (aiCharacter.getPersonalityTraits() == null || aiCharacter.getPersonalityTraits().isEmpty()) {
            return "请选择至少一个性格特点";
        }
        if (aiCharacter.getInterests() == null || aiCharacter.getInterests().isEmpty()) {
            return "请选择至少一个兴趣爱好";
        }
        if (aiCharacter.getBackgroundStory() == null || aiCharacter.getBackgroundStory().trim().isEmpty()) {
            return "请输入角色背景故事";
        }
        return null;
    }

    /**
     * 获取角色类型列表
     * @return 角色类型列表
     */
    public List<String> getCharacterTypes() {
        List<String> types = new ArrayList<>();
        types.add("情感陪伴");
        types.add("学习助手");
        types.add("职业导师");
        types.add("知心朋友");
        types.add("心理咨询");
        types.add("创意伙伴");
        types.add("自定义");
        return types;
    }

    public void loadCharacterById(String characterId) {
        characterRepository.loadCharacterById(characterId, new CharacterRepository.DataCallback<AiCharacter>() {
            @Override
            public void onSuccess(AiCharacter data) {
                currentCharacter.postValue(data);
                isLoading.postValue(false);
            }

            @Override
            public void onError(String msg) {
                errorMessage.postValue(msg);
                isLoading.postValue(false);
            }

            @Override
            public void onNetworkError() {
                errorMessage.postValue("网络错误，请稍后重试");
                isLoading.postValue(false);
            }
        });
    }

    /**
     * 获取自定义角色列表
     */
    public LiveData<List<AiCharacter>> getAiCharacters() {
        return aiCharacters;
    }

    /**
     * 获取加载状态
     */
    public LiveData<Boolean> getIsLoading() {
        return isLoading;
    }

    /**
     * 获取创建成功状态
     */
    public LiveData<Boolean> getCreateSuccess() {
        return createSuccess;
    }

    /**
     * 获取错误信息
     */
    public LiveData<String> getErrorMessage() {
        return errorMessage;
    }

    /**
     * 获取当前角色
     */
    public LiveData<AiCharacter> getCurrentCharacter() {
        return currentCharacter;
    }
}